﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Localization;

namespace Tomahawk.Runtime.LoadingScreens
{
    public sealed class UIImage : UIControl
    {
        #region Serializable Properties
        [XmlAttribute("FlipHorizontally")]
        public bool FlipHorizontally = false;

        [XmlAttribute("FlipVertically")]
        public bool FlipVertically = false;

        [XmlElement("Color")]
        public Color Color = Color.White;

        [XmlElement("Texture")]
        public string TextureName = "";
        #endregion

        #region Properties
        private string localizedTextureName = null;
        private Texture2D texture = null;
        #endregion

        #region Loading
        internal override void Load(ContentManager contentManager, LocalizationManager localizationManager)
        {
            if (Localized)
            {
                localizationManager.GetLocalizedString(TextureName, out localizedTextureName);
            }
            else
            {
                localizedTextureName = TextureName;
            }

            texture = contentManager.Load<Texture2D>(localizedTextureName);
            
            base.Load(contentManager, localizationManager);
        }
        #endregion

        #region Localization
        internal override void Localize(ContentManager contentManager, LocalizationManager localizationManager)
        {
            if (Localized)
            {
                string oldTextureName = localizedTextureName;
                if (string.Compare(oldTextureName, localizedTextureName, true) != 0)
                {
                    texture.Dispose();
                    texture = contentManager.Load<Texture2D>(localizedTextureName);
                }
            }

            base.Localize(contentManager, localizationManager);
        }
        #endregion

        #region Render
        internal override void Render(UICanvas canvas, SpriteBatch spriteBatch)
        {
            if (!texture.IsDisposed)
            {
                Color imageColor = Color.White;
                imageColor.R = (byte)((canvas.overlayColor.R * Color.R) / 255.0f);
                imageColor.G = (byte)((canvas.overlayColor.G * Color.G) / 255.0f);
                imageColor.B = (byte)((canvas.overlayColor.B * Color.B) / 255.0f);
                imageColor.A = (byte)((canvas.overlayColor.A * Color.A) / 255.0f);

                SpriteEffects effect = SpriteEffects.None;
                effect |= FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
                effect |= FlipVertically ? SpriteEffects.FlipVertically : SpriteEffects.None;

                canvas.ApplyVirtualCanvas(ref RenderRectangle, ref ScreenRectangle);
                spriteBatch.Draw(texture, ScreenRectangle, null, imageColor, RenderRotation, RenderOrigin, effect, this.Depth);
            }
        }
        #endregion
    }
}
